package com.ai.common;

import com.ai.module.chat.config.advice.exception.BusinessException;
import com.ai.module.chat.util.DateUtils;
import com.ai.module.mj.constant.stringConstant.ReturnCode;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.http.HttpStatus;

import static com.ai.module.mj.constant.stringConstant.ReturnCode.FAILURE;

/**
 * &#064;author:  MrHe
 * {@code @create:} 2023-11-23 21:26
 * {@code @description:}
 **/
@Data
@NoArgsConstructor
public class BaseResp <T>{

    int code;

    String message;

    String currentTime= DateUtils.getTransDateTime();

    T data;

    public static<T> BaseResp<T> success(T data){
        BaseResp<T> resp = new BaseResp<>();
        resp.setCode(200);
        resp.setMessage("success");
        resp.setData(data);
        return resp;
    }

    public static<T> BaseResp<T> success(){
        BaseResp<T> resp = new BaseResp<>();
        resp.setCode(200);
        resp.setMessage("success");
        resp.setData(null);
        return resp;
    }

    public static<T> BaseResp<T> fail(T data){
        BaseResp<T> resp = new BaseResp<>();
        resp.setCode(500);
        resp.setMessage("fail");
        resp.setData(data);
        return resp;
    }
    //
    public static<T> BaseResp<T> fail(T data, HttpStatus status){
        BaseResp<T> resp = new BaseResp<>();
        resp.setCode(status.value());
        resp.setMessage("fail");
        resp.setData(data);
        return resp;
    }
    public static<T> BaseResp<T> fail(T data,int code){
        BaseResp<T> resp = new BaseResp<>();
        resp.setCode(code);
        resp.setMessage("fail");
        resp.setData(data);
        return resp;
    }




    public static <T> BaseResp<T> success(int code, String description, T result) {
        return new BaseResp<>(code, description, result);
    }

    public static <Y> BaseResp<Y> validationError() {
        return new BaseResp<>(ReturnCode.VALIDATION_ERROR, "校验错误");
    }

    public static <Y> BaseResp<Y> failure() {
        return new BaseResp<>(ReturnCode.FAILURE, "系统异常");
    }

    public static <Y> BaseResp<Y> failure(String description) {
        return new BaseResp<>(ReturnCode.FAILURE, description);
    }

    public static <Y> BaseResp<Y> of(int code, String description) {
        throw new BusinessException(description, FAILURE);
    }

    public static <T> BaseResp<T> of(int code, String description, T result) {
        return new BaseResp<>(code, description, result);
    }

    private BaseResp(int code, String message) {
        this(code, message, null);
    }

    private BaseResp(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

}
